/* eslint-disable */
import { fileURLToPath, URL } from "node:url";

import vue from "@vitejs/plugin-vue";
import path from 'path'
import vueJsx from "@vitejs/plugin-vue-jsx";
import { loadEnv } from 'vite'
import viteCompression from 'vite-plugin-compression'
import { createSvgIconsPlugin } from 'vite-plugin-svg-icons'
import AutoImport from 'unplugin-auto-import/vite'
import Components from 'unplugin-vue-components/vite'
import { ElementPlusResolver } from 'unplugin-vue-components/resolvers'

// https://vitejs.dev/config/
export default ({ command, mode }) => {
  const env = loadEnv(mode, process.cwd())
  console.log(mode, env)
  return {
    base: mode === 'production' ? '/' : '/',
    define: {
      'process.platform': null,
      'process.env': {
        buildTime: new Date().getTime(),
        NODE_ENV: mode,
        ...env
      },
      'process.version': null
    },
    clearScreen: false,
    server: {
      hmr: { overlay: false }, // 禁用或配置 HMR 连接 设置 server.hmr.overlay 为 false 可以禁用服务器错误遮罩层
      // 服务配置
      port: 82, // 类型： number 指定服务器端口;
      host: true,
      open: false, // 类型： boolean | string在服务器启动时自动在浏览器中打开应用程序；
      cors: true, // 类型： boolean | CorsOptions 为开发服务器配置 CORS。默认启用并允许任何源
      proxy: {
        '/api': {
          target: 'http://dict.goyu.store/thai_dict_backend',
          changeOrigin: true,
          rewrite: path => path.replace(/^\/api/, ''),
          secure: false
        }
      }
    },
    plugins: [
      vue(),
      vueJsx(),
      viteCompression({
        // 生成压缩包gz
        verbose: true,
        disable: false,
        threshold: 10240,
        algorithm: 'gzip',
        ext: '.gz',
      }),
      AutoImport({
        resolvers: [ElementPlusResolver()],
      }),
      Components({
        resolvers: [ElementPlusResolver()],
      }),
      createSvgIconsPlugin({
        iconDirs: [path.resolve(process.cwd(), 'src/assets/icons/svg')],
            symbolId: 'icon-[dir]-[name]',
            svgoOptions: mode === 'production' 
        })
    ],
    build: {
      assetsDir: 'static',
      rollupOptions: {
        output: { //静态资源分类打包
          chunkFileNames: 'static/js/[name]-[hash].js',
          entryFileNames: 'static/js/[name]-[hash].js',
          assetFileNames: 'static/[ext]/[name]-[hash].[ext]',
        }
      }
    },
    resolve: {
      alias: {
        "@": fileURLToPath(new URL("./src", import.meta.url)),
      },
    },
    css: {
      // css sass  charset 报错
      postcss: {
        plugins: [
            {
              postcssPlugin: 'internal:charset-removal',
              AtRule: {
                charset: (atRule) => {
                  if (atRule.name === 'charset') {
                    atRule.remove();
                  }
                }
              }
            }
        ],
      },
      preprocessorOptions: {
        // 定义全局scss变量
        scss: {
          // src\assets\css\common.scss
          additionalData: `@import "@/assets/css/common.scss";`,
          // charset: false
        }
      }
    },
  }
}


